
import axios from 'axios'

import { Message } from 'element-ui'
import cookie from 'js-cookie'

// 创建axios实例
const service = axios.create({
  baseURL: 'http://10.18.25.94:9001', // api的base_url
  // baseURL: 'http://10.21.1.147:9001/',
  // baseURL: 'http://www.igotcha.top:9001/',
  timeout: 20000, // 请求超时时间
})

service.interceptors.request.use(config => {
  if (cookie.get('guli_token')) {
    config.headers.token = cookie.get('guli_token')
  }
  return config
}, err => {
  return Promise.reject(err)
})

service.interceptors.response.use(response => {
  if (response.data.code === 28004) {
    window.location.href = '/login'
  } else if (response.data.code !== 20000) {
    if (response.data.code !== 25000) {
      Message({
        message: response.data.message || 'error',
        type: 'error',
        duration: 5 * 1000
      })
    }
  } else {
    return response
  }
}, error => {
  return Promise.reject(error.response)
})

export default service